<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Add_truck
 *
 * @author admin
 */
class add_repair extends MY_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->model('repair_model');
        $this->load->model('truck/truck_model');
        $this->load->model('stock/stock_model');
        $this->load->model('catalog');
    }

    public function index() {
        $this->_set_form();
        $this->load->view('repair/add_repair', $this->data);
    }

    private function _set_form() {
        $utility = new Config_utility();

        $day = date('d');
        $month = date('m');
        $year = date('Y') + 543;

        $this->data['cmb_license_plate'] = form_dropdown('cmbLicensePlate', $this->repair_model->list_truck_name(), set_value('cmbLicensePlate'), 'class="form-control"');
        $this->data['cmb_repair_type'] = form_dropdown('cmbRepairType', $this->catalog->list_repair_type(), set_value('cmbRepairType'), 'class="form-control"');
        $this->data['txt_repair_detail'] = form_textarea(array('name' => 'txtRepairDetail', 'rows' => '5'), set_value('txtRepairDetail'), 'class="form-control" style="resize: none;"');
        $this->data['cmb_day'] = form_dropdown('cmbDay', $utility->list_day(), $day, 'class="form-control"');
        $this->data['cmb_month'] = form_dropdown('cmbMonth', $utility->list_month(), $month, 'class="form-control"');
        $this->data['cmb_year'] = form_dropdown('cmbYear', $utility->list_year(), $year, 'class="form-control"');
        $this->data['list_main_type'] = $this->catalog->list_stock_main_type();
        $this->data['list_sub_type'] = $this->catalog->list_stock_sub_type();
        $this->data['list_brand'] = $this->catalog->list_stock_brand();

        $this->_set_error();
    }

    private function _set_error() {
        $this->data['err_license_plate'] = form_error('cmbLicensePlate', '<div class="error">', '</div>');
        $this->data['err_repair_type'] = form_error('cmbRepairType', '<div class="error">', '</div>');
        $this->data['err_repair_detail'] = form_error('txtRepairDetail', '<div class="error">', '</div>');
        $this->data['err_day'] = form_error('cmbDay', '<div class="error">', '</div>');
        $this->data['err_month'] = form_error('cmbMonth', '<div class="error">', '</div>');
        $this->data['err_year'] = form_error('cmbYear', '<div class="error">', '</div>');
    }

    public function validate() {
        $utility = new Config_form();
        $this->form_validation->set_rules('cmbDay', 'Day', 'required');
        $this->form_validation->set_rules('cmbMonth', 'Month', 'required');
        $this->form_validation->set_rules('cmbYear', 'Year', 'required');
        $this->form_validation->set_rules('cmbLicensePlate', 'License Plate Code', 'required');
        $this->form_validation->set_rules('cmbRepairType', 'Type', 'required');
        $this->form_validation->set_rules('txtRepairDetail', 'Repairing Detail', 'required');

        $licenPlateId = $this->input->post('cmbLicensePlate');
        $repairType = $this->input->post('cmbRepairType');
        $repairDetail = $this->input->post('txtRepairDetail');
        $arrConfirm = $this->input->post('hidArrayConfirm');

        $arrExplode = explode(",", $arrConfirm);

        if ($this->form_validation->run() == FALSE) {
            $this->_set_form();
            $this->load->view('repair/add_repair', $this->data);
        } else {

            $day = $this->input->post('cmbDay');
            $month = $this->input->post('cmbMonth');
            $year = $this->input->post('cmbYear');
            $date_create = "";
            if ($day != "" && $month != "" && $year != "") {
                $date_create = $year . "/" . $month . "/" . $day;
            }

            $data = $this->truck_model->get_truck_by_id($licenPlateId);
            $this->repair_model->save_data($licenPlateId, $data[0]->driver_id, $repairType, $repairDetail, $date_create);
            $last_data = $this->repair_model->get_last_data_repair();

            if ($arrExplode[0] !== '') {
                foreach ($arrExplode as $index) {
                    $main_type_id = $this->input->post('cmbMainType' . $index);
                    $sub_type_id = $this->input->post('cmbSubType' . $index);
                    $brand = $this->input->post('cmbBrand' . $index);
                    $autopart_name = $this->input->post('txtAutoPart' . $index);
                    $dataStock = $this->stock_model->get_stock_by_name($main_type_id, $sub_type_id, $brand, $autopart_name);
                    $qty = $this->input->post('txtQty' . $index);
                    $this->repair_model->save_order_data($last_data[0]->id, $main_type_id, $sub_type_id, $brand, $dataStock[0]->id, $qty, ($dataStock[0]->stock_price * $qty));
                    if ($repairType == 5) {//สถานะรายการซ่อม
                        $this->stock_model->update_stock($dataStock[0]->id, ($dataStock[0]->stock_qty - $qty));
                    } else {
                        $this->stock_model->update_stock($dataStock[0]->id, ($dataStock[0]->stock_qty + $qty));
                    }
                }
            }
            echo $utility->form_alert_success('บันทึกเสร็จเรียบร้อย', 'repair/home');
            exit();
        }
    }

    public function chk_autopart() {
        $main_type_id = $this->input->post('main_type_id');
        $sub_type_id = $this->input->post('sub_type_id');
        $brand_id = $this->input->post('brand_id');
        $value = $this->input->post('name');
        $qty = $this->input->post('qty');
        $stock = $this->stock_model->get_stock_by_name($main_type_id, $sub_type_id, $brand_id, $value);
        $array = array();
        if (count($stock) > 0) {
            $array['result_name'] = "TRUE";
            $array['result_qty'] = (($stock[0]->stock_qty - $qty) < 0) ? 'FALSE' : 'TRUE';
        } else {
            $array['result_name'] = "FALSE";
        }
        echo json_encode($array);
    }

}
